package io.renren.modules.three;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import io.renren.common.utils.DateUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ReadAndWriteDoc {

    public static void main(String[] args) throws Exception {
        Map<String, Object> datas = new HashMap<String, Object>() {
            {
                //字符文件
                put("cf_2019", "zhangsan");
                //网络图片
                put("cf_2108", new PictureRenderData(200, 100, ".png",
                        BytePictureUtils.getUrlByteArray("https://pulilai.oss-cn-beijing.aliyuncs.com/qianming/20191008/cb88741838724e94910c4b2d572a6820_333")));
            }
        };
        readwriteWord("C:\\Users\\40938\\Desktop\\wxTools\\doc\\installConfirm.docx", datas);
    }

    /**
     * 实现对word读取和修改操作
     * @param filePath    word模板路径和名称
     * @param datas        待填充的数据，从数据库读取
     */
    public static void readwriteWord(String filePath, Map<String, Object> datas) throws Exception {
        for (Map.Entry<String, Object> entry : datas.entrySet()) {
            if(entry.getValue().equals("1"))
                datas.put(entry.getKey(), "是");
            else if(entry.getValue().equals("2"))
                datas.put(entry.getKey(), "否");
            else if(entry.getValue().equals("3"))
                datas.put(entry.getKey(), "满意");
            else if(entry.getValue().equals("4"))
                datas.put(entry.getKey(), "一般");
            else if(entry.getValue().equals("5"))
                datas.put(entry.getKey(), "不满意");
            else if(entry.getValue().equals("6"))
                datas.put(entry.getKey(), "手动");
            else if(entry.getValue().equals("7"))
                datas.put(entry.getKey(), "自动");
            else
                continue;
        }
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(datas);
        FileOutputStream out = new FileOutputStream(File.separator + "www" + File.separator + "server" + File.separator
                + "apache-tomcat-8.5.32" + File.separator + "webapps" + File.separator + "wxTools" + File.separator + "files"
                + File.separator + DateUtils.format(new Date(), "yyyyMMddHHmmss") + "_" + datas.get("cm000034name")
                + ".docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

}
